package com.example.foo.account.dao;

import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.account.model.Account;
import com.example.foo.account.query.AccountQuery;

@org.apache.ibatis.annotations.Mapper
public interface AccountDao extends com.example.core.dao.BaseDao<Account, Long> {

	public default PageResult<Account> findAccounts(AccountQuery accountQuery, Pageable pageable) {
		QueryWrapper<Account> queryWrapper = createQueryWithBaseIf(accountQuery);
		//
		if (accountQuery.getAcctc() != null) {
			queryWrapper.eq("acctc", accountQuery.getAcctc());
		}
		if (accountQuery.getDrcrbal() != null) {
			queryWrapper.eq("drcrbal", accountQuery.getDrcrbal());
		}
		if (accountQuery.getUserId() != null) {
			queryWrapper.eq("user_id", accountQuery.getUserId());
		}
		return findPages(queryWrapper, pageable);
	}

	@Update("UPDATE account "
			+ "SET "
				+ "total = total + #{total}, "
				+ "updated_at = #{updatedAt} "
			+ "WHERE id = #{id}")
	public int updateAccountTotalIncrement(Account account);

	@Update("UPDATE account "
			+ "SET "
				+ "total = total - #{total}, "
				+ "updated_at = #{updatedAt} "
			+ "WHERE id = #{id} AND total>=#{total}")
	public int updateAccountTotalDecrement(Account account);

}
